package com.android.equipment.adapter

import android.graphics.Color
import android.widget.TextView
import com.android.equipment.R
import com.android.equipment.bean.RealDataBean
import com.android.equipment.dao.operate.DaoRealTimeAlam
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class DeviceNodeAdapter(private val workId: Long, private val deviceCode: String): BaseMultiItemQuickAdapter<RealDataBean,BaseViewHolder>() {
    var isHaveAlam = false
    init {
        addItemType(0,R.layout.item_device_node_one)
        addItemType(1,R.layout.item_device_node_two)
    }

    override fun convert(holder: BaseViewHolder, item: RealDataBean) {


        when(holder.itemViewType){
            0 -> {
                holder.setText(R.id.tv_node_des,"${item.node_des}：")
                holder.setText(R.id.tv_node_value, "${item.node_value}${item.node_unit}")
            }

            1 -> {
                holder.setText(R.id.tv_node_des,"${item.node_des}：")
                if (item.node_value.isEmpty()){
                    holder.setText(R.id.tv_node_value, "--")
                }else {
                    holder.setText(R.id.tv_node_value, "${item.node_value}${item.node_unit}")
                }

                if (!item.relation_node_value.isNullOrEmpty()){
                    holder.setGone(R.id.tv_relation_node_value, false)
                    holder.setGone(R.id.tv_relation_node_des, false)
                    holder.setText(R.id.tv_relation_node_des, "${item.relation_node_des}：")
                    holder.setText(R.id.tv_relation_node_value, "${item.relation_node_value}${item.relation_node_unit}")
                }else {
                    holder.setGone(R.id.tv_relation_node_value, true)
                    holder.setGone(R.id.tv_relation_node_des, true)
                }


            }
        }

//        if (workStatus == 1){
            val realTimeAlamList = DaoRealTimeAlam.getInstance().getRealTimeAlamByWorkIdAndDeviceCode(workId, deviceCode)
            if (realTimeAlamList.isNotEmpty()){
                val map = realTimeAlamList.map { it.node }

                if (isHaveAlam){
                    holder.itemView.setBackgroundResource(if (map.contains(item.node)) R.drawable.shape_radius_8_solid_ffeae8 else R.drawable.shape_radius_8_solid_white_stroke_1_f0f2f5)
                }else {
                    holder.itemView.setBackgroundResource(R.drawable.shape_radius_8_solid_white_stroke_1_f0f2f5)
                }


                if (holder.itemViewType == 0){
                    holder.getView<TextView>(R.id.tv_node_des).setTextColor(if (map.contains(item.node)) Color.parseColor("#ED3241") else Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_node_value).setTextColor(if (map.contains(item.node)) Color.parseColor("#ED3241") else Color.parseColor("#1D2129"))
                }else {
                    holder.getView<TextView>(R.id.tv_node_des).setTextColor(if (map.contains(item.node)) Color.parseColor("#ED3241") else Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_node_value).setTextColor(if (map.contains(item.node)) Color.parseColor("#ED3241") else Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_relation_node_des).setTextColor(if (map.contains(item.node)) Color.parseColor("#ED3241") else Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_relation_node_value).setTextColor(if (map.contains(item.node)) Color.parseColor("#ED3241") else Color.parseColor("#1D2129"))
                }

            }else {
                holder.itemView.setBackgroundResource(R.drawable.shape_radius_8_solid_white_stroke_1_f0f2f5)
                if (holder.itemViewType == 0){
                    holder.getView<TextView>(R.id.tv_node_des).setTextColor(Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_node_value).setTextColor(Color.parseColor("#1D2129"))
                }else {
                    holder.getView<TextView>(R.id.tv_node_des).setTextColor(Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_node_value).setTextColor(Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_relation_node_des).setTextColor(Color.parseColor("#1D2129"))
                    holder.getView<TextView>(R.id.tv_relation_node_value).setTextColor(Color.parseColor("#1D2129"))
                }

            }
//        }else {
//            holder.itemView.setBackgroundResource(R.drawable.shape_radius_100_solid_white)
//            if (holder.itemViewType == 0){
//                holder.getView<TextView>(R.id.tv_node_value).setTextColor(Color.parseColor("#4E5969"))
//            }else {
//                holder.getView<TextView>(R.id.tv_node_name).setTextColor(Color.parseColor("#4E5969"))
//                holder.getView<TextView>(R.id.tv_node_value).setTextColor(Color.parseColor("#4E5969"))
//            }
//        }



    }


     fun isHaveAlam(isHave: Boolean){
         isHaveAlam = isHave
     }

}